﻿using System;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Windows.Controls;
using System.Xml;

namespace SomethingSpacial.Presenters
{
    /// <summary>
    /// The FeedPresenter class is designed to call RSS and like feeds for information
    /// and to agregate that data and present it to the view.
    /// </summary>
    public class FeedPresenter
    {
        private ItemsControl _Target;
        private ItemsControl _Target2;
        private int _DisplayCount = 1;
        private int _DisplayCount2 = 1;

        public void PresentAFeed(ItemsControl Target, string TargetAddress, int DisplayCount)
        {
            _Target = Target;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri(TargetAddress));
            _DisplayCount = DisplayCount;
        }

        public void PresentA2Feed(ItemsControl Target, ItemsControl Target2, string TargetAddress, int DisplayCount, int DisplayCount2)
        {
            _Target = Target;
            _Target2 = Target2;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client2_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri(TargetAddress));
            _DisplayCount2 = DisplayCount2;
            _DisplayCount = DisplayCount;
        } 

        void client2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(e.Result))
                {
                    XmlReader feedReader = XmlReader.Create(new StringReader(e.Result));
                    SyndicationFeed feed = SyndicationFeed.Load(feedReader);
                    _Target.ItemsSource = feed.Items.Take(_DisplayCount);
                    _Target2.ItemsSource = feed.Items.Take(_DisplayCount2);
                }
            }
            catch (Exception) { }
        }

        public void PresentMainFeed(ItemsControl Target)
        {
            _Target = Target;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri(App.MainFeed.Address));
            _DisplayCount = 1;
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(e.Result))
                {
                    XmlReader feedReader = XmlReader.Create(new StringReader(e.Result));
                    SyndicationFeed feed = SyndicationFeed.Load(feedReader);
                    _Target.ItemsSource = feed.Items.Take(_DisplayCount);
                }
            }
            catch (Exception) { }
        }
    }
}
